﻿using System;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;

namespace breakoutsharp.input
{
    public class DxMouse : IDisposable
    {
        private Device device = null;
        private bool disposed = false;

        public DxMouse(System.Windows.Forms.Form form)
        {
            device = new Device(SystemGuid.Mouse);
            device.SetCooperativeLevel(form.Handle, CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
            device.SetDataFormat(DeviceDataFormat.Mouse);

            try
            {
                device.Acquire();
            }
            catch (DirectXException)
            {
                throw;
            }
        }

        public MouseState State
        {
            get
            {
                return ((MouseState)device.GetDeviceState(typeof(MouseState)));
            }
            set
            {
            }
        }

        public void Poll()
        {
            try
            {
                device.Poll();

            }
            catch (Exception)
            {
                
                throw;
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (!(device == null))
                    {
                        device.Unacquire();
                    }

                }
                disposed = true;
            }
        }

        #endregion
    }
}
